package com.web.transaction.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sun.deploy.net.URLEncoder;
import com.web.common.BaseResult;
import com.web.common.ReturnCode;
import com.web.common.enums.BaseStatus;
import com.web.config.ExcelStylerUtil;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.web.config.RedisUtil;
import com.web.transaction.entity.Transaction;
import com.web.transaction.service.TransactionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - TransactionController",description = "transaction")
@RestController
@CrossOrigin
@RequestMapping("/transaction")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;


    @RequestMapping(value = "/addTransaction",method = RequestMethod.POST)
    @ApiOperation(value = "新增transaction")
    public BaseResult addTransaction(@RequestBody Transaction transaction){
        transactionService.addTransaction(transaction);
        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.FCE_INSERT_SUCCESS.getDesc());
    }

    @RequestMapping(value = "/transactionList")
    public BaseResult<List<Transaction>> transactionList(@ModelAttribute Transaction transaction){
    	List<Transaction> transactionList = transactionService.getTransactions(transaction);
    	return new BaseResult<>(BaseStatus.SUCCESS.getCode(),transactionList);
    }

    @RequestMapping(value = "/export")
    public void exportTransaction(@ModelAttribute Transaction transaction, HttpServletResponse response) throws IOException {
        List<Transaction> transactionList = transactionService.getTransactions(transaction);

        String fileName = "核心交易数据";
        String sheetName = "Transaction交易数据";
        ExportParams exportParams = new ExportParams(fileName,sheetName, ExcelType.XSSF);
        exportParams.setStyle(ExcelStylerUtil.class);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Transaction.class, transactionList);


        //将表格输出
        fileName = URLEncoder.encode(fileName,"UTF8");
        //设置返回数据类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        workbook.write(response.getOutputStream());
    }

    @RequestMapping(value = "/getEcharts")
    public BaseResult getEchatsList(){

        List<Map<String,String>> transactionMap= transactionService.getEcharts();

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),transactionMap);
    }

}
